var util = {
    isUrl: function(str){
        return !!str.match(/^(https?|ftp|file):\/\/[-a-zA-Z0-9+&@#\/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#\/%=~_|]/);
    },
    getDomain: function(url){
        var domain = url.match(/\/\/.*?\.(.*?)\//);
        return domain?domain[1]:"";
    },
    batDownload: function(links,getRealLink){
        var realLinks = [];
        if(getRealLink){
            for(var i = 0; i < links.length; i++){
                (function(link){
                    var xhr = new XMLHttpRequest();
                    xhr.onreadystatechange = function(){
                        if (xhr.readyState==4){// 4 = "loaded"
                            if (xhr.status==200) {// 200 = OK
                                var realLink = getRealLink(xhr.responseText);
                                var domain = util.getDomain(realLink);
                                parseRules[domain] && parseRules[domain](realLink);
                            } else {
                            }
                        }
                    }
                    xhr.open("GET",link,true);
                    xhr.send();
                })(links[i])
            }
        }
    },
    addFixedPositionLink: function(url, title, img){
        if($("#fixedPositionLink").length > 0){
            $("#fixedPositionLink").attr("href", url).attr("title", title);
            return;
        }
        var a = $("<a id='fixedPositionLink' target='_blank' title='"+title+"'></a>").appendTo($("body"));
        if(typeof url == "string"){

            a.attr("href", url);
        }else if(typeof url == "function"){
            a.bind("click", url);
        }
        a.css({
                "position":"fixed",
                "display":"block",
                "right":"50px",
                "top": "100px",
                "border": "solid 2px",
                "border-color": "darkgreen",
                "border-radius": "3px",
                "background-color":"white",
                "cursor":"pointer"
            })
        if(img){
            a.append($("<img src="+img+">"));
            var img = a.find("img");
            var deg = 0;
            var opacity = 1;
            var p = -0.1;
            var hoverFlg = false;
            var interval = setInterval(function(){
                // if(opacity <= 0.6){
                //     p = 0.1;
                // }else if(opacity >= 1){
                //     p = -0.1;
                // }
                // img.css({"opacity": opacity+=p});
                if(!hoverFlg){
                    img.css({"-webkit-transform": 'rotateY('+deg+'deg)'});
                    deg++;
                }
            },20);
            img.hover(function(){
                hoverFlg = true;
                img.css({"-webkit-transform": 'rotateY(0deg)'});
                clearInterval(interval);
                deg = 0;
            }, function(){
                hoverFlg = false;
            })
        }else{
            a.text("下载");
        }
        util.drag.add(a, function(){return false}, a);
        return a;
    },
    drag: {
        position: {},
        add: function(b, c, d) {
            var e = $(b), f = $(d), g = !1, h = 0, i = 0, j = 0, k = 0, hrefFlg = false;
            var dragedEle = $(b);
            e.bind("mousedown", function(b) {
                $(document).bind("mousemove", function(a) {
                    if(!hrefFlg && "A" == dragedEle[0].tagName && (Math.abs(a.pageY - i) > 3 || Math.abs( - a.pageX + h) > 3)){
                        var href = dragedEle.attr("href");
                        dragedEle.attr("href", "javascript:void(0)").attr("_href", href);
                        hrefFlg = true;
                    }
                    f.css({top: k + a.pageY - i,left: j + a.pageX - h,right:right - a.pageX + h}), g = !0
                })
                return 1 != b.which ||  c && c(b.target) ? void 0 : (h = b.pageX, i = b.pageY, j = parseInt(d.css("left")), right = parseInt(d.css("right")), k = parseInt(d.css("top")), !1)
            }), $(document).bind("mouseup", function(b) {
                setTimeout(function(){
                    var _href = dragedEle.attr("_href");
                    dragedEle.attr("href", _href);
                    hrefFlg = false;
                },50)
                return $(document).unbind("mousemove"), g ? !1 : void 0
            })
        },
        remove: function(b) {
            var c = $(b);
            c.unbind("mousedown")
        },
        onmousedown: function(b) {
            $(document).bind("mouseover", function() {
                _this.dragEle.css("left", b.pageX).css("top", b.pageY)
            })
        },
        onmouseup: function() {
            $(document).unbind("mouseover", onmouseover)
        },
        onmouseover: function(a) {
            _this.dragEle.css("left", a.pageX).css("top", a.pageY)
        }
    },
    getJSON: function(url, callback){
        window.jsonp123456 = callback;
        var ele = document.createElement("script");
        ele.src=url+"?callback=jsonp123456";
        document.head.appendChild(ele);
    },
    inArray: function( elem, arr) {
        var len;
        var i = 0;

        if ( arr ) {
            len = arr.length;
            for ( ; i < len; i++ ) {
                // Skip accessing in sparse arrays
                if ( i in arr && arr[ i ] === elem ) {
                    return i;
                }
            }
        }

        return -1;
    },
    getRespText: function(url, callback){
        var respText = "";
        $.ajax( {
            url:url,
            async:true,
            success: function(respText){
                callback(respText);
            }
        });
    },
    getPicUrl: function(respText, reg){
        var result = null;
        var picUrls = [];
        while(1){
            result = reg.exec(respText);
            if(!result){
                break;
            }else{
                picUrls.push(result[1]);
            }
        }
        return picUrls;
    }
}
